

package org.example.datastream.test;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.AscendingTimestampExtractor;
import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

/**
 * Skeleton for a Flink Streaming Job.
 *
 * <p>For a tutorial how to write a Flink streaming application, check the
 * tutorials and examples on the <a href="https://flink.apache.org/docs/stable/">Flink Website</a>.
 *
 * <p>To package your application into a JAR file for execution, run
 * 'mvn clean package' on the command line.
 *
 * <p>If you change the name of the main class (with the public static void main(String[] args))
 * method, change the respective entry in the POM.xml file (simply search for 'mainClass').
 */
public class Job5 {

	public static void main(String[] args) throws Exception {
		// set up the streaming execution environment
		final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

		/*
		 * Here, you can start creating your execution plan for Flink.
		 *
		 * Start with getting some data from the environment, like
		 * 	env.readTextFile(textPath);
		 *
		 * then, transform the resulting DataStream<String> using operations
		 * like
		 * 	.filter()
		 * 	.flatMap()
		 * 	.join()
		 * 	.coGroup()
		 *
		 * and many more.
		 * Have a look at the programming guide for the Java API:
		 *
		 * https://flink.apache.org/docs/latest/apis/streaming/index.html
		 *
		 */

//		String filePath = "D:\\playground\\Flink-demo\\src\\main\\resources\\keyword.txt";
//		DataStreamSource<String> txt = env.readTextFile(filePath);
//		txt.print();

		env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);

		List<User> list = new ArrayList<>();
		List<User> list2 = new ArrayList<>();
		IntStream.iterate(1, x -> x + 1)
				.limit(100)
				.forEach(x -> list.add(new User(x, "user-" + x, LocalDateTime.now())));
		IntStream.iterate(10, x -> x + 1)
				.limit(20)
				.forEach(x -> list2.add(new User(x, "user-" + x, LocalDateTime.now())));

		DataStreamSource<User> stream1 = env.fromCollection(list);
		DataStreamSource<User> stream2 = env.fromCollection(list2);



		KeyedStream<User, Integer> keyedStream1 = stream1.keyBy(x -> x.getId());
		KeyedStream<User, Integer> keyedStream2 = stream2.keyBy(x -> x.getId());

		keyedStream1.assignTimestampsAndWatermarks(WatermarkStrategy.forBoundedOutOfOrderness(Duration.ofSeconds(10)));
		keyedStream2.assignTimestampsAndWatermarks(WatermarkStrategy.forBoundedOutOfOrderness(Duration.ofSeconds(10)));

		keyedStream1.assignTimestampsAndWatermarks(new AscendingTimestampExtractor<User>() {
			@Override
			public long extractAscendingTimestamp(User user) {
				return user.getDate().toEpochSecond(ZoneOffset.ofHours(8));
			}
		});
		keyedStream2.assignTimestampsAndWatermarks(new AscendingTimestampExtractor<User>() {
			@Override
			public long extractAscendingTimestamp(User user) {
				return user.getDate().toEpochSecond(ZoneOffset.ofHours(8));
			}
		});

		keyedStream1.intervalJoin(keyedStream2)
				.inEventTime()
				.between(Time.seconds(-2), Time.seconds(2))
				.upperBoundExclusive()
				.lowerBoundExclusive()
				.process(new ProcessJoinFunction<User, User, Object>() {
					@Override
					public void processElement(User user, User user2, Context context, Collector<Object> collector) throws Exception {
						collector.collect(new Tuple2<User,User>(user, user2));
					}
				})
				.print();

		// execute program
		env.execute("Flink Streaming Java API Skeleton");
	}
	public static class User {
		private int id;
		private String name;
		private LocalDateTime date;

		public User(int id, String name, LocalDateTime date) {
			this.id = id;
			this.name = name;
			this.date = date;
		}

		public void setId(int id) {
			this.id = id;
		}

		public void setName(String name) {
			this.name = name;
		}

		public void setDate(LocalDateTime date) {
			this.date = date;
		}

		public int getId() {
			return id;
		}

		public String getName() {
			return name;
		}

		public LocalDateTime getDate() {
			return date;
		}

		@Override
		public String toString() {
			return "User{" +
					"id=" + id +
					", name='" + name + '\'' +
					", date=" + date +
					'}';
		}
	}
	static class score {
		private int id;
		private int score;

		public score(int id, int score) {
			this.id = id;
			this.score = score;
		}

		public int getId() {
			return id;
		}

		public int getScore() {
			return score;
		}
	}
}
